package com.avit.tv.kui;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.avit.tv.kui.base.BaseFocusLayout;
import com.avit.tv.kui.utils.Utils;

/**
 * 带功能条目
 *
 * @author like
 * @date 2020/6/10
 */
public class AvitToolbar extends BaseFocusLayout {

    private boolean isShowLeft;
    private boolean isShowRight;
    private int leftIconColor;
    private int rightIconColor;
    private int midTextColor;
    private int rightTextColor;
    private int leftTextColor;
    private Drawable leftIcon;
    private Drawable rightIcon;
    private int leftIconSize;
    private int rightIconSize;
    private int leftTextSize;
    private int midTextSize;
    private int rightTextSize;
    private String leftText;
    private String rightText;
    private String midText;
    private AvitPartChildMarqueeView leftTextView;
    private AvitPartChildMarqueeView midTextView;
    private AvitPartChildMarqueeView rightTextView;
    private ImageView leftIconView;
    private ImageView rightIconView;

    /**
     * 处理子控件显示的布局
     */
    public enum ViewPosition {
        LEFT, MID, RIGHT
    }

    public AvitToolbar(@NonNull Context context) {
        super(context);
    }

    public AvitToolbar(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public AvitToolbar(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void initView(Context context, AttributeSet attrs, int defAttr) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs,
                R.styleable.AvitToolbar, defAttr, 0);
        isShowLeft = typedArray.getBoolean(R.styleable.AvitToolbar_is_show_left, false);
        isShowRight = typedArray.getBoolean(R.styleable.AvitToolbar_is_show_right, false);
        leftIconColor = typedArray.getColor(R.styleable.AvitToolbar_left_icon_color, Color.TRANSPARENT);
        rightIconColor = typedArray.getColor(R.styleable.AvitToolbar_right_icon_color, Color.TRANSPARENT);
        midTextColor = typedArray.getColor(R.styleable.AvitToolbar_mid_text_color, Color.WHITE);
        rightTextColor = typedArray.getColor(R.styleable.AvitToolbar_right_text_color, Color.WHITE);
        leftTextColor = typedArray.getColor(R.styleable.AvitToolbar_left_text_color, Color.WHITE);
        leftIcon = typedArray.getDrawable(R.styleable.AvitToolbar_left_image_icon);
        rightIcon = typedArray.getDrawable(R.styleable.AvitToolbar_right_image_icon);
        leftIconSize = typedArray.getDimensionPixelSize(R.styleable.AvitToolbar_left_icon_size, DEFAULT_SIZE);
        rightIconSize = typedArray.getDimensionPixelSize(R.styleable.AvitToolbar_right_icon_size, DEFAULT_SIZE);
        leftTextSize = typedArray.getDimensionPixelSize(R.styleable.AvitToolbar_left_text_size, DEFAULT_SIZE);
        midTextSize = typedArray.getDimensionPixelSize(R.styleable.AvitToolbar_mid_text_size, DEFAULT_SIZE);
        rightTextSize = typedArray.getDimensionPixelSize(R.styleable.AvitToolbar_right_text_size, DEFAULT_SIZE);
        leftText = typedArray.getString(R.styleable.AvitToolbar_left_text);
        rightText = typedArray.getString(R.styleable.AvitToolbar_right_text);
        midText = typedArray.getString(R.styleable.AvitToolbar_mid_text);
        typedArray.recycle();
    }

    @Override
    protected void initContent(Context context) {
        leftTextView = new AvitPartChildMarqueeView(context);
        midTextView = new AvitPartChildMarqueeView(context);
        rightTextView = new AvitPartChildMarqueeView(context);
        leftIconView = new ImageView(context);
        rightIconView = new ImageView(context);
        initTextView(leftTextView, leftTextSize, leftTextColor, leftText);
        initTextView(midTextView, midTextSize, midTextColor, midText);
        initTextView(rightTextView, rightTextSize, rightTextColor, rightText);
        if (leftIconColor != Color.TRANSPARENT)
            Utils.setSvgImageRes(leftIconView, leftIcon, leftIconColor);
        if (rightIconColor != Color.TRANSPARENT)
            Utils.setSvgImageRes(rightIconView, rightIcon, rightIconColor);
        leftIconView.setImageDrawable(leftIcon);
        rightIconView.setImageDrawable(rightIcon);
    }

    @Override
    protected void insertView() {
        if (isShowLeft) {
            showIcon(leftIconSize, leftIconView);
            leftTextView.setMinWidth(leftIconSize);
        }
        if (isShowRight) {
            showIcon(rightIconSize, rightIconView);
            rightTextView.setMinWidth(rightIconSize);
        }
        addView(leftTextView);
        addView(rightTextView);
        addView(midTextView);
    }


    private void showIcon(int size, View iconView) {
        LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        layoutParams.width = size;
        layoutParams.height = size;
        if (getHashViewCode(iconView).equals(getHashViewCode(leftIconView)))
            layoutParams.gravity = Gravity.START;
        else
            layoutParams.gravity = Gravity.END;
        iconView.setLayoutParams(layoutParams);
        addView(iconView);
    }


    private void initTextView(TextView textView, int size, int color, String value) {
        textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
        textView.setTextColor(color);
        textView.setText(value);
        LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
        if (getHashViewCode(textView).equals(getHashViewCode(leftTextView)))
            layoutParams.gravity = Gravity.START;
        else if (getHashViewCode(textView).equals(getHashViewCode(midTextView))) {
            layoutParams.gravity = Gravity.CENTER;
        } else
            layoutParams.gravity = Gravity.END;
        textView.setLayoutParams(layoutParams);
        textView.setGravity(Gravity.CENTER);
    }


    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        initMax();
    }


    private void initMax() {
        int width = getWidth();
        if (width == 0) return;
        leftTextView.setMaxWidth(Math.round(width / 3f));
        rightTextView.setMaxWidth(Math.round(width / 3f));
        if (leftTextView.getText().length() > 0 || rightTextView.getText().length() > 0)
            midTextView.setMaxWidth(Math.round(2 * width / 3f));
        if (leftTextView.getText().length() > 0 && rightTextView.getText().length() > 0)
            midTextView.setMaxWidth(Math.round(width / 3f));
    }

    /**
     * 更新子控件的焦点状态,锁定焦点于何处
     *
     * @param position MID: 中间 textView ， LEFT： 左侧 textView/imageView ， RIGHT: 右侧 textView/imageView
     */
    public void initChildFocus(ViewPosition position) {
        switch (position) {
            case MID:
                if (midTextView != null)
                    midTextView.requestFocus();
                break;
            case LEFT:
                if (isShowLeft)
                    leftIconView.requestFocus();
                else if (leftTextView != null)
                    leftTextView.requestFocus();
                break;
            case RIGHT:
                if (isShowRight)
                    rightIconView.requestFocus();
                else if (rightTextView != null)
                    rightTextView.requestFocus();
                break;
        }
    }

    /**
     * 设置是否显示左侧图标【左侧文字可以不输入值即可不显示】
     *
     * @param showLeft
     */
    public void setShowLeft(boolean showLeft) {
        isShowLeft = showLeft;
        if (isShowLeft) leftIconView.setVisibility(VISIBLE);
        else leftIconView.setVisibility(GONE);
    }

    /**
     * 设置是否显示右侧图标【右侧文字可以不输入值从而不显示】
     *
     * @param showRight
     */
    public void setShowRight(boolean showRight) {
        isShowRight = showRight;
        if (isShowRight) rightIconView.setVisibility(VISIBLE);
        else rightIconView.setVisibility(GONE);
    }

    /**
     * 设置左侧图标颜色
     *
     * @param leftIconColor
     */
    public void setLeftIconColor(@ColorInt int leftIconColor) {
        this.leftIconColor = leftIconColor;
        Utils.setSvgImageRes(leftIconView, leftIcon, leftIconColor);
    }

    /**
     * 设置右侧图标颜色
     *
     * @param rightIconColor
     */
    public void setRightIconColor(@ColorInt int rightIconColor) {
        this.rightIconColor = rightIconColor;
        Utils.setSvgImageRes(rightIconView, rightIcon, rightIconColor);
    }

    /**
     * 设置中间文字颜色
     *
     * @param midTextColor
     */
    public void setMidTextColor(@ColorInt int midTextColor) {
        this.midTextColor = midTextColor;
        midTextView.setTextColor(midTextColor);
    }

    /**
     * 设置右侧文字颜色
     *
     * @param rightTextColor
     */
    public void setRightTextColor(@ColorInt int rightTextColor) {
        this.rightTextColor = rightTextColor;
        rightTextView.setTextColor(rightTextColor);
    }

    /**
     * 设置左侧文字颜色
     *
     * @param leftTextColor
     */
    public void setLeftTextColor(@ColorInt int leftTextColor) {
        this.leftTextColor = leftTextColor;
        leftTextView.setTextColor(leftTextColor);
    }

    /**
     * 设置左侧图标
     *
     * @param leftIcon
     */
    public void setLeftIcon(Drawable leftIcon) {
        this.leftIcon = leftIcon;
        leftIconView.setImageDrawable(leftIcon);
    }

    /**
     * 设置右侧图标
     *
     * @param rightIcon
     */
    public void setRightIcon(Drawable rightIcon) {
        this.rightIcon = rightIcon;
        rightIconView.setImageDrawable(rightIcon);
    }

    /**
     * 设置左侧图标大小
     *
     * @param leftIconSize
     */
    public void setLeftIconSize(int leftIconSize) {
        this.leftIconSize = leftIconSize;
        leftIconView.setMaxHeight(leftIconSize);
        leftIconView.setMaxWidth(leftIconSize);
    }

    /**
     * 设置右侧图标大小
     *
     * @param rightIconSize
     */
    public void setRightIconSize(int rightIconSize) {
        this.rightIconSize = rightIconSize;
        rightIconView.setMaxWidth(rightIconSize);
        rightIconView.setMaxWidth(rightIconSize);
    }

    /**
     * 设置左侧文字尺寸
     *
     * @param leftTextSize
     */
    public void setLeftTextSize(int leftTextSize) {
        this.leftTextSize = leftTextSize;
        leftTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, leftTextSize);
    }

    /**
     * 设置中间文字尺寸
     *
     * @param midTextSize
     */
    public void setMidTextSize(int midTextSize) {
        this.midTextSize = midTextSize;
        midTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, midTextSize);
    }

    /**
     * 设置右侧文字尺寸
     *
     * @param rightTextSize
     */
    public void setRightTextSize(int rightTextSize) {
        this.rightTextSize = rightTextSize;
        rightTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, rightTextSize);
    }

    /**
     * 设置左侧文字内容
     *
     * @param leftText
     */
    public void setLeftText(String leftText) {
        this.leftText = leftText;
        leftTextView.setText(leftText);
    }

    /**
     * 设置右侧文字内容
     *
     * @param rightText
     */
    public void setRightText(String rightText) {
        this.rightText = rightText;
        rightTextView.setText(rightText);
    }

    /**
     * 设置中间文字内容
     *
     * @param midText
     */
    public void setMidText(String midText) {
        this.midText = midText;
        midTextView.setText(midText);
    }

    /**
     * 是否开启子testView 跑马灯的自动滚动【毋须焦点对上】
     * @param isOpenAutoMarquee
     */
    public void initAutoMarquee(boolean isOpenAutoMarquee) {
        leftTextView.setFocused(!isOpenAutoMarquee);
        rightTextView.setFocused(!isOpenAutoMarquee);
        midTextView.setFocused(!isOpenAutoMarquee);
    }

}
